package cz.muni.fi.pa165.ws;

import java.util.List;

import cz.muni.fi.pa165.sportmanager.burnedcaloriesws.BurnedCalories;
import cz.muni.fi.pa165.sportmanager.burnedcaloriesws.CreateBurnedCaloriesRequest;
import cz.muni.fi.pa165.sportmanager.burnedcaloriesws.CreateBurnedCaloriesResponse;
import cz.muni.fi.pa165.sportmanager.burnedcaloriesws.DeleteBurnedCaloriesRequest;
import cz.muni.fi.pa165.sportmanager.burnedcaloriesws.DeleteBurnedCaloriesResponse;
import cz.muni.fi.pa165.sportmanager.burnedcaloriesws.FindAllBurnedCaloriesRequest;
import cz.muni.fi.pa165.sportmanager.burnedcaloriesws.FindAllBurnedCaloriesResponse;
import cz.muni.fi.pa165.sportmanager.burnedcaloriesws.FindBurnedCalorieRequest;
import cz.muni.fi.pa165.sportmanager.burnedcaloriesws.FindBurnedCalorieResponse;
import cz.muni.fi.pa165.sportmanager.burnedcaloriesws.UpdateBurnedCaloriesRequest;
import cz.muni.fi.pa165.sportmanager.burnedcaloriesws.UpdateBurnedCaloriesResponse;
import cz.muni.fi.pa165.ws.adapter.IBurnedCaloriesAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

/**
 * @author forman
 */
@Endpoint
public class BurnedCaloriesWSEndpoint {

	private static final String NAMESPACE = "http://muni.cz/fi/pa165/sportManager/burnedCaloriesWS";

	@Autowired
	private IBurnedCaloriesAdapter burnedCaloriesAdapter;

	@PayloadRoot(localPart = "CreateBurnedCaloriesRequest", namespace = NAMESPACE)
	@ResponsePayload
	CreateBurnedCaloriesResponse
	createBurnedCalories(@RequestPayload final CreateBurnedCaloriesRequest createBurnedCaloriesRequest) {
		burnedCaloriesAdapter.createBurnedCalories(createBurnedCaloriesRequest.getBurnedCalories());
		return new CreateBurnedCaloriesResponse();
	}

	@PayloadRoot(localPart = "DeleteBurnedCaloriesRequest", namespace = NAMESPACE)
	@ResponsePayload
	DeleteBurnedCaloriesResponse
	deleteBurnedCalories(@RequestPayload final DeleteBurnedCaloriesRequest deleteBurnedCaloriesRequest) {
		burnedCaloriesAdapter.deleteBurnedCalories(deleteBurnedCaloriesRequest.getBurnedCalories());
		return new DeleteBurnedCaloriesResponse();
	}

	@PayloadRoot(localPart = "UpdateBurnedCaloriesRequest", namespace = NAMESPACE)
	@ResponsePayload
	UpdateBurnedCaloriesResponse
	updateBurnedCalories(@RequestPayload final UpdateBurnedCaloriesRequest updateBurnedCaloriesRequest) {
		burnedCaloriesAdapter.updateBurnedCalories(updateBurnedCaloriesRequest.getBurnedCalories());
		return new UpdateBurnedCaloriesResponse();
	}

	@PayloadRoot(localPart = "FindBurnedCaloriesRequest", namespace = NAMESPACE)
	@ResponsePayload
	FindBurnedCalorieResponse
	findBurnedCalories(@RequestPayload final FindBurnedCalorieRequest findBurnedCalorieRequest) {
		final BurnedCalories burnedCalories = burnedCaloriesAdapter.findBurnedCalories(Long.valueOf(findBurnedCalorieRequest.getBurnedCaloriesId()));
		final FindBurnedCalorieResponse findBurnedCalorieResponse = new FindBurnedCalorieResponse();
		findBurnedCalorieResponse.setBurnedCalories(burnedCalories);
		return findBurnedCalorieResponse;
	}

	@PayloadRoot(localPart = "FindAllBurnedCaloriesRequest", namespace = NAMESPACE)
	@ResponsePayload
	FindAllBurnedCaloriesResponse
	findAllBurnedCalories(@RequestPayload final FindAllBurnedCaloriesRequest findAllBurnedCaloriesRequest) {
		final List<BurnedCalories> allBurnedCalories = burnedCaloriesAdapter.findAllBurnedCalories();
		final FindAllBurnedCaloriesResponse findAllBurnedCaloriesResponse = new FindAllBurnedCaloriesResponse();
		findAllBurnedCaloriesResponse.getBurnedCalories().addAll(allBurnedCalories);
		return findAllBurnedCaloriesResponse;
	}
}
